#!/bin/bash

#filename color_install.sh

#usage: used to install colorscheme for vim

cd ${VIM_CONFIG_HOME:="$HOME/gitworkspace/.dotfiles/lib/vim/"}

 sys_color_dir=$HOME/.vim/colors
 local_color_dir=$VIM_CONFIG_HOME/_vim/colors
 local_vim_dir=$VIM_CONFIG_HOME/_vim

if [[ $1  ]]; then

    echo "called by outter"
    local_color_dir=./_vim/colors
    local_vim_dir=./_vim
fi


#create colors directory for system vim runtime path
if [ ! -d $sys_color_dir ]; then
	echo "creating colors dir for vim"
	mkdir -p $HOME/.vim/colors;
fi

#==========================================================
#function used to install colorschema into local color dir
#==========================================================
function Install_Git_Color_To_Local {

    git_host="https://github.com"
    color_rep=$1
    color_name=$2

   
    if [ ! -f $local_color_dir/$color_name ]; then
        echo "$local_color_dir/$color_name  not exists needs to install"
        echo "Color Install Beginning!"
#        clone the git rep into _vim/
        git clone $git_host/$color_rep $local_vim_dir/$color_rep

#        move the files under color_rep dire to local colors dir
        if [[ -f $local_vim_dir/$color_rep/colors/$color_name  ]]; then

            echo "found color schema file :" $local_vim_dir/$color_rep/colors/$color_name 
            mv $local_vim_dir/$color_rep/colors/$color_name $local_color_dir
        else
            echo "not found color schema file :" $local_vim_dir/$color_rep/colors/$color_name 
        fi

#        copy the color file name to system vim colors
#        cp $local_color_dir/$color_name  $sys_color_dir

#        remove the other file from git rep 
        rm -rf $local_vim_dir/$color_rep

        echo "Color Install Done!"
    fi
}


#==========================================================
# invoke function to install color from github to local 
# colors dir
#==========================================================
Install_Git_Color_To_Local "tomasr/molokai.git"  "molokai.vim"

Install_Git_Color_To_Local "altercation/vim-colors-solarized.git" "solarized.vim"


#==========================================================
#copy the colors you have local into system vim colors
#==========================================================
for f in $(ls $local_color_dir); do
    if [[ ! -f $sys_color_dir/$f  ]]; then
        echo "color schema file $f none exists, copy from local color dir"
        cp $local_color_dir/$f $sys_color_dir
    fi
done

